package weekMatch.one_102;

import java.util.Arrays;

public class LeeCode_03_02 {
    public static void main(String[] args) {
        System.out.println(miceAndCheese(new int[]{1, 1, 3, 4}, new int[]{4, 4, 1, 1}, 2));
        System.out.println(miceAndCheese(new int[]{1,1}, new int[]{1,1}, 2));
        System.out.println(miceAndCheese(new int[]{4,1,5,3,3}, new int[]{3,4,4,5,2}, 3));
        System.out.println(miceAndCheese(new int[]{1,2,2,2,1,1}, new int[]{1,2,1,1,1,1}, 2));
        System.out.println(miceAndCheese(new int[]{4,1,4,3,2,3}, new int[]{4,4,4,1,4,1}, 3));
        System.out.println(miceAndCheese(new int[]{1,1,4,1,2,3}, new int[]{1,2,4,1,3,3}, 4));
        System.out.println(miceAndCheese(new int[]{1}, new int[]{4}, 1));
    }
    private static int miceAndCheese(int[] reward1, int[] reward2, int k) {
        Integer[] flag = new Integer[reward1.length];
        int[] sub = new int[reward1.length];
        for (int i = 0; i < reward1.length; i++) {
            flag[i] = i;
        }
        for (int i = 0; i < reward1.length; i++) {
            sub[i] = reward1[i] - reward2[i];
        }
        Arrays.sort(flag, (o1, o2) -> sub[o2] - sub[o1]);
        int ans = 0;
        for (int i = 0; i < flag.length; i++) {
            if (i < k){
                ans += reward1[flag[i]];
            }else {
                ans += reward2[flag[i]];
            }
        }
        return ans;
    }
}
